package org.smart4j.framework.dwtest.simplproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.smart4j.framework.core.impl.support.ClassTemplate;

import java.lang.reflect.Method;

/**
 * Created by
 * <p>
 * cglib是针对method级别的代理 Object reback = methodProxy.invokeSuper(o, objects);
 *
 * @author dw
 * @date 2018/01/24
 */
public class CglibProxy implements MethodInterceptor {
    private static CglibProxy instance = new CglibProxy();

    private CglibProxy() {
    }

    public static CglibProxy getInstance() {
        return instance;
    }

    public <T> T getProxy(Class<T> cls) {
        return (T) Enhancer.create(cls, this);
    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib方法拦截,方法名称:" + method.getName());
        if (method.getName().startsWith("say")) {
            System.out.println("cglib方法拦截判断正在执行say方法");
        }
        Object reback = methodProxy.invokeSuper(o, args);
        return reback;
    }
}
